home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 151_01 / ed2.c < prev    next >
Text File  |  1985-03-09  |  12KB  |  607 lines

  1. /* ED2.C */
  2.  
  3. #include "ed0.c"
  4. #include "ed1.ccc"
  5. #define SIGNON "E.K. Ream/Dr Dobb's Editor - IBM PC enhancement: Mar. 10, 1984"
  6. #define SIGNNN "            Modified by Alan D. Howard"
  7. #define HELP1 "open <f/n> | name <f/n> | rename <f/n> | delname <f/n>"
  8. #define HELP2 "load <f/n> | append <f/n> | rest | read <n> | write <n> | save"
  9. #define HELP3 "extract [<f t>] | closeread | closewrite"
  10. #define HELP4 "clear | delete [<f t>] | move [<f t n>] | copy [<f t n>]"
  11. #define HELP5 "find | search [<f t>] | change [<f t>] | list [<f t>]"
  12. #define HELP6 "tabs <n> | showtab | hidetab | help | dos | g <n> | count <n>"
  13. #define CMNDMODE    1
  14. #define INSMODE     2
  15. #define EDITMODE    3
  16. #define EXITMODE    4
  17. int    begin_line, end_line, buf_picked;
  18. main(argc,argv) char *argv[];
  19. {
  20. int mode;
  21. int i;
  22.     sysinit();
  23.     fmtassn(NO);
  24.     fmtset(8);
  25.     fmtntab();
  26.     outclr();
  27.     outxy(0,SCRNL1);
  28.     message(SIGNON);
  29.     message(SIGNNN);
  30.     outxy(0,1);
  31.     fileclear();    /* make sure no read or write files indicated */
  32.     bufnew();
  33.     begin_line=(-1);    /* imaginary to start with */
  34.     end_line=(-1);
  35.     buf_picked=NO;
  36.     mode=CMNDMODE;
  37.     if (argc>1) {
  38.         openf(argv[1],NO);
  39.         rest(argv[0]);
  40.         outxy(0,1);
  41.     }
  42.     edgetln();
  43.     while(1){
  44.         if (mode ==EXITMODE) {
  45.             sysfinish();
  46.             break;
  47.         }
  48.         else if (mode==CMNDMODE) {
  49.             mode=command();
  50.         }
  51.         else if (mode==EDITMODE) {
  52.             mode=edit();
  53.         }
  54.         else if (mode==INSMODE) {
  55.             mode=insert();
  56.         }
  57.         else {
  58.             syserr("main: no mode");
  59.             mode=EDITMODE;
  60.         }
  61.     }
  62. }
  63. edit()
  64. {
  65. char c;
  66.     pmtedit();
  67.     while(1){
  68.         c=syscin();    /* tolower eliminated */
  69.         if (c==ESC1) {        /* enter command mode */
  70.             return(CMNDMODE);
  71.         }
  72.         else if (c==INS1) {    /* enter insert mode */
  73.             return(INSMODE);
  74.         }
  75.         else if (special(c)==YES) {
  76.             if (c==UP1){    /* DOWN1 now treated separately */
  77.                 return(INSMODE);
  78.             }
  79.             else {
  80.                 continue;
  81.             }
  82.         }
  83.         else if (c==DOWN1) {    /* DOWN1 now does not     */
  84.             eddn();     /* enter insert mode but */
  85.             pmtline();    /* just advances to next line */
  86.             edbegin();
  87.             pmtcol();
  88.         }
  89.         else if (c==DTOCH){    /* kill to character entered */
  90.             pmtmode("edit:    kill");
  91.             c=syscin();
  92.             if ((special(c)==NO) &
  93.                 (control(c)==NO)) {
  94.                 edkill(c);
  95.             }
  96.             pmtedit();
  97.         }
  98.         else if (c==GTOCH){    /* search to character entered */
  99.             pmtmode("edit: search");
  100.             c=syscin();
  101.             if ((special(c)==NO) &
  102.                 (control(c)==NO)) {
  103.                 edsrch(c);
  104.             }
  105.             pmtedit();
  106.         }
  107.         else {        /* editor now exchanges any other character */
  108.             if ((special(c)==NO) &    /* with cursor */
  109.                 (control(c)==NO)) {
  110.                 edchng(c);
  111.             }
  112.             pmtcol();
  113.         }
  114.     }
  115. }
  116. insert()
  117. {
  118. char c;
  119.     pmtmode("insert");
  120.     while(1) {
  121.         c=syscin();
  122.         if (c==ESC1) {
  123.             return(CMNDMODE);
  124.         }
  125.         else if (c==EDIT1) {
  126.             return(EDITMODE);
  127.         }
  128.         else if (c==INS1) {
  129.             ;
  130.         }
  131.         else if (c==DOWN1) {    /* in insert mode DOWN1 treated  */
  132.             ednewdn();    /* differently from edit mode     */
  133.             pmtline();    /* inserts new line */
  134.             continue;
  135.         }
  136.         else if (c==DTOCH){    /* kill to character entered */
  137.             pmtmode("Insert: kill");
  138.             c=syscin();
  139.             if ((special(c)==NO) &
  140.                 (control(c)==NO)) {
  141.                 edkill(c);
  142.             }
  143.         }
  144.         else if (c==GTOCH){    /* search to character entered */
  145.             pmtmode("Insert: search");
  146.             c=syscin();
  147.             if ((special(c)==NO) &
  148.                 (control(c)==NO)) {
  149.                 edsrch(c);
  150.             }
  151.         }
  152.         else if (special(c)==YES) {
  153.             continue;
  154.         }
  155.         else if (control(c)==YES) {
  156.             continue;
  157.         }
  158.         else {
  159.             edins(c);
  160.             pmtcol();
  161.         }
  162.     }
  163. }
  164. control(c) char c;
  165. {
  166.     if (c==TAB) {
  167.         return(NO);
  168.     }
  169.     else if (c>=127) {
  170.         return(YES);
  171.     }
  172.     else if (c<32) {
  173.         return(YES);
  174.     }
  175.     else {
  176.         return(NO);
  177.     }
  178. }
  179. special(c) char c;
  180. {
  181. char sbuffer[SCRNW1];
  182. int v,x,y;
  183. int k;
  184.     if (c==JOIN1) {
  185.         edjoin();
  186.         pmtline();
  187.         return(YES);
  188.     }
  189.     if (c==SPLT1) {
  190.         edsplit();
  191.         pmtline();
  192.         return(YES);
  193.     }
  194.     if (c==ABT1) {
  195.         edabt();
  196.         pmtcol();
  197.         return(YES);
  198.     }
  199.     else if (c==LFTDEL) {        /* delete to left of cursor */
  200.         edldel();
  201.         pmtcol();
  202.         return(YES);
  203.     }
  204.     else if (c==DEL1) {        /* delete at cursor */
  205.         edcdel();
  206.         pmtcol();
  207.         return(YES);
  208.     }
  209.     else if (c==ZAP1) {
  210.         edzap();
  211.         pmtline();
  212.         return(YES);
  213.     }
  214.     else if (c==UP2) {
  215.         edup();
  216.         pmtline();
  217.         return(YES);
  218.     }
  219.     else if (c==UP1) {
  220.         ednewup();
  221.         pmtline();
  222.         return(YES);
  223.     }
  224.  
  225. /* DOWN1 (CR) is no longer a special character */
  226.  
  227.     else if (c==DOWN2) {
  228.         eddn();
  229.         pmtline();
  230.         return(YES);
  231.     }
  232.     else if (c==ABSLEFT) {        /* left one column */
  233.         edabsleft();
  234.         pmtcol();
  235.         return(YES);
  236.     }
  237.     else if (c==ABSRGHT) {         /* right one column */
  238.         edabsright();
  239.         pmtcol();
  240.         return(YES);
  241.     }
  242.     else if (c==LEFT1) {          /* left one character */
  243.         edleft();
  244.         pmtcol();
  245.         return(YES);
  246.     }
  247.     else if (c==RIGHT1) {         /* right one character */
  248.         edright();
  249.         pmtcol();
  250.         return(YES);
  251.     }
  252.     else if (c==BEGINPL) {        /* mark beginning line */
  253.         begin_line=bufln();
  254.         pmtmode("marked buff top");
  255.         return(YES);
  256.         }
  257.     else if (c==ENDPL) {        /* mark ending line */
  258.         end_line=bufln();
  259.         pmtmode("marked buff end");
  260.         return(YES);
  261.         }                                   
  262.     else if (c==PICKIT) {        /* pick marked lines to buffer */
  263.         if (edpick(begin_line,end_line)!=ERR) {
  264.             pmtmode("buffer picked");
  265.             buf_picked=YES;
  266.             }
  267.         return(YES);
  268.         }
  269.     else if (c==RIGHT1) {    /* go to end of line */
  270.         edright();
  271.         pmtcol();
  272.         return(YES);
  273.     }
  274.     else if (c==ERASE) {    /* erase to end of line */
  275.         ederase();
  276.         pmtcol();
  277.         return(YES);
  278.     }
  279.     else if (c==HOME) {    /* move cursor alternatingly to */
  280.         edhome();    /* top and bottom of screen */
  281.         pmtline();
  282.         sysabort();
  283.         return(YES);
  284.     }
  285.     else if (c==LSTRT) {    /* move to beginning of line */
  286.         edbegin();
  287.         pmtcol();
  288.         return(YES);
  289.     }
  290.     else if (c==PUTIT) {   /* put buffer before line*/
  291.         if (buf_picked==YES) {
  292.             edput();
  293.             begin_line=(-1);
  294.             end_line=(-1);
  295.             }
  296.         pmtline();
  297.         return(YES);
  298.     }
  299.     else if (c==DSCROL) {    /* scroll down */
  300.         pmtmode("Scroll down");
  301.         while (bufnrbot()==NO) {
  302.             if (chkkey()==YES) {
  303.                 break;
  304.             }
  305.             if (eddn()==ERR) {
  306.                 break;
  307.             }
  308.         }
  309.         pmtline();
  310.         return(YES);
  311.     }
  312.     else if (c==LEND){    /* move to end of line */
  313.         edend();
  314.         pmtcol();
  315.         return(YES);
  316.     }
  317.     else if (c==PAGEUP) {  /* move up 20 lines */
  318.         y=bufln();
  319.         y=max(y-20,1);
  320.         if (bufattop()!=YES) {
  321.             edgo(y,0);
  322.         }
  323.         pmtline();
  324.         return(YES);
  325.     }
  326.     else if (c==PAGEDN) {   /*move down 20 lines */
  327.         y=bufln();
  328.         y=y+20;
  329.         if (bufnrbot()!=YES) {
  330.             edgo(y,0);
  331.         }
  332.         pmtline();
  333.         return(YES);
  334.     }
  335.     else if (c==GOTO){    /* go to line entered */
  336.         x=outxget();
  337.         y=outyget();
  338.         pmtcmnd("Goto: ",sbuffer);
  339.         if(number(sbuffer,&v)) {
  340.             edgo(v,0);
  341.         }
  342.         else {
  343.             outxy(x,y);
  344.         }
  345.         return(YES);
  346.     }
  347.     else if (c==USCROL) {    /* scroll up */
  348.         pmtmode("Scroll up");
  349.         while (bufattop()==NO) {
  350.             if (chkkey()==YES) {
  351.                 break;
  352.             }
  353.             if (edup()==ERR) {
  354.                 break;
  355.             }
  356.         }
  357.         pmtline();
  358.         return(YES);
  359.     }
  360.     else  {
  361.         return(NO);
  362.     }
  363. }
  364. command()
  365. {
  366. int v;
  367. char c;
  368. char args[SCRNW1];
  369. char *argp;
  370. int topline;
  371. int ypos;
  372. int oldline;
  373. int k;
  374.     edrepl();
  375.     oldline=bufln();
  376.     ypos=outyget();
  377.     topline=oldline-ypos+1;
  378.     while(1) {
  379.         outxy(0,SCRNL1);
  380.         fmtcrlf();
  381.         pmtmode("command:");
  382.         getcmnd(args,0);
  383.         fmtcrlf();
  384.         pmtline();
  385.         c=args[0];
  386.         if ((c==EDIT1)|(c==INS1)) {
  387.             if (oldline==bufln()) {
  388.                 edgetln();
  389.                 bufout(topline,1,SCRNL1);
  390.                 outxy(0,ypos);
  391.             }
  392.             else {
  393.                 edgo(bufln(),0);
  394.             }
  395.             if (c==EDIT1) {
  396.                 return(EDITMODE);
  397.             }
  398.             else {
  399.                 return(INSMODE);
  400.             }
  401.         }
  402.         else if (tolower(args[0])=='g'){
  403.             argp=skipbl(args+1);
  404.             if (argp[0]==EOS) {
  405.                 edgo(oldline,0);
  406.                 return(EDITMODE);
  407.             }
  408.             else if (number(argp,&v)==YES) {
  409.                 edgo(v,0);
  410.                 return(EDITMODE);
  411.             }
  412.             else {
  413.                 message("bad line number");
  414.             }
  415.         }
  416.         else if (lookup(args,"append")) {
  417.             append(args);
  418.         }
  419.         else if (lookup(args,"change")) {
  420.             change(args,begin_line,end_line);
  421.         }
  422.         else if (lookup(args,"clear")) {
  423.             sysabort();
  424.             clear();
  425.         }
  426.         else if (lookup(args,"count")) { /* repeat count for replay */ 
  427.             count(args);
  428.         }
  429.         else if (look